#include "stdafx.h"
#include "Input.h"

namespace Reign
{namespace Input
{namespace DI
{
	#pragma region Properties
	IDirectInput8* Input::Device::get() {return device;}
	#pragma endregion

	#pragma region Constructors
	Input::Input(DisposableI^ parent, Window^ window)
	: Disposable(parent)
	{
		init(parent, window->Handle);
	}

	Input::Input(DisposableI^ parent, IntPtr handle)
	: Disposable(parent)
	{
		init(parent, handle);
	}

	void Input::init(DisposableI^ parent, IntPtr handle)
	{
		null();
		try
		{
			this->handle = handle;

			IDirectInput8 *deviceTemp;
			if (FAILED(DirectInput8Create(GetModuleHandle(0), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&deviceTemp, 0)))
			{
				device = 0;
				throw(gcnew Exception("Reign.Input.DI: Failed at DirectInput8Create"));
			}
			device = deviceTemp;
		}
		catch (Exception^ ex)
		{
			delete this;
			throw ex;
		}
	}

	Input::~Input()
	{
		disposeChilderen();
		if (device) device->Release();
		null();
	}

	void Input::null()
	{
		device = 0;
	}
	#pragma endregion
	
	#pragma region Methods
	void Input::Update()
	{
		if (UpdateCallback != nullptr) UpdateCallback();
	}
	#pragma endregion
}
}
}